[Assess] Coastal Livelihoods Case Study

📍 Isle de Jean Charles

2025-10-03

Class Overview

  • Centering activity
  • Setting the stage
  • Paired coding

Centering Activity

Setting the Stage

  • This week’s focus is on plotting data!
  • Using archived data from federal organizations such as:
    • Federal Emergency Management Agency (FEMA)
    • Department of Transportation (DOT)
    • Centers for Disease Control (CDC)
    • National Oceanic and Atmospheric Administration (NOAA)

Learning Outcomes

  • Explore aesthetic mappings
  • Use different geom functions to visualize variables
  • Apply statistical transformations and position adjustments
  • Connect findings to theoretical understandings of recognition justice

Let’s get Started

  • Find your pair
  • Open Posit

Load packages

  • Remember that we only need to install packages once
    • paste the commented code into your console to install packages you don’t yet have.
    • include lines 4-7 in your analysis.qmd document
# install.packages("mapview")
# install.packages("sf")
# install.packages("webshot2")
library(tidyverse)
library(readxl)
library(mapview)
library(sf)

The Grammar of Graphics

  • Any graphic can be built using the same components:
    • data set (what we visualize)
    • coordinate system (where the data should go)
    • geom (how the data should be visualized)

Today’s Recipe

Where we’re headed

Load data

LA_tracts <- read_sf("data/LA_tracts/LA_tracts.shp")
# | eval: true
# | echo: false
LA_tracts <- read_sf("~/Library/CloudStorage/GoogleDrive-mc152@wellesley.edu/My Drive/Teaching/ES-202/Case Study Code/data/week_05/LA_tracts/LA_tracts.shp")

Understanding our data

  • Let’s take a look at how this data looks.
glimpse(LA_tracts)
Rows: 1,376
Columns: 10
$ state    <chr> "Louisiana", "Louisiana", "Louisiana", "Louisiana", "Louisian…
$ county   <chr> "Lafayette", "Lafayette", "Vermilion", "St. Martin", "St. Mar…
$ gegrphy  <chr> "LA", "LA", "LA", "LA", "LA", "LA", "LA", "LA", "LA", "LA", "…
$ pct_sth  <dbl> 4.1503823, 8.1000000, 1.6978710, 4.1154232, 5.7540218, 4.2769…
$ pct_cnc  <dbl> 3.7503771, 7.1000000, 1.0837475, 2.3294546, 4.0278153, 2.5407…
$ pc_200_  <dbl> 30.14978, 19.73158, 31.13836, 18.35729, 35.25180, 46.30960, 3…
$ trnsp_b  <dbl> 0.1909443, 0.1220533, 0.2269403, 0.1586263, 0.1301868, 0.2927…
$ cf_nn__  <chr> "0", "0", "0", "0", "2-4", "0", "0", "0", "0", "0", "0", "0",…
$ trnsp__  <chr> "15-20%", "10-15%", "20-25%", "15-20%", "10-15%", "25+%", "20…
$ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-92.0536 30..., MULTIPOLYGON (((…

Looking at spatial data

  • the geometry variable data in our dataframe and the .shp file type of data suggests this is spatial data.
  • let’s quickly map things, to see what data we have. I’ll visualize the gegrphy column using the mapview() function.
LA_tracts |>
  mapview(zcol = "gegrphy")

Tidying our dataframe

  • Spatial data usually has very short variable names.
  • Let’s use the rename() function to fix that, and the st_drop_geometry() function to remove the spatial component of our dataframe.
  • We’ll create a new dataframe called LA, and remove the old one LA_tracts using the rm() function.
LA <- LA_tracts |>
  rename(
    "geography" = "gegrphy",
    "pct_asthma" = "pct_sth",
    "pct_cancer" = "pct_cnc",
    "pct_200_fpl" = "pc_200_",
    "transp_burd" = "trnsp_b",
    "transp_burd_cat" = "trnsp__",
    "ann_cf_cat" = "cf_nn__"
  ) |>
   mutate(geography = factor(geography, levels = c("Isle de Jean Charles", "Terrebonne", "LA"))) |>
  st_drop_geometry()

rm(LA_tracts)  

Aesthetic Mappings (color)

  • Aesthetic mappings allow you to choose things like the x and y variables you’d like to visualize, and the colors, shapes, and outlines you’d like to visualize.
    • Suppose we wanted to visualize the relationship between pct_cancer and pct_asthma, by geography.
    • we use a geom_point() geom to visualize the relationship, define our x and y axes, and define color to show the geography group.
LA |>
ggplot() +
  geom_point(aes(x = pct_cancer, y = pct_asthma, color = geography))

Aesthetic Mappings (shape)

  • We can run the same lines of code and change the mapping value from color to shape.
  • This admittedly makes a very bad plot.
LA |>
ggplot() +
  geom_point(aes(x = pct_cancer, y = pct_asthma, shape = geography))

Aesthetic Mappings (size)

  • We can run the same line of code and change the mapping value to size.
  • This admittedly makes an even worse plot.
LA |>
ggplot() +
  geom_point(aes(x = pct_cancer, y = pct_asthma, size = geography))

Aesthetic Mappings (transparency)

  • We can run the same line of code and change the mapping value to alpha.
  • This admittedly makes an equally terrible plot.
LA |>
ggplot() +
  geom_point(aes(x = pct_cancer, y = pct_asthma, alpha = geography))

EX_01

  • Using the LA dataframe, can you plot the relationship between the percent of families in a tract living 200% below the federal poverty line (pct_200_fpl), and the their transportation burden (transp_burd)?
  • use a geom_point() function
  • choose an aesthetic to visualize the geography variable.
  • What do your graph results suggest? Is there anything you might change about this graphic to make it more accessible to your readers?

Geometric Objects

  • Geometric objects determine how we will visualize our data.
    • The previous examples used geom_point().
    • Let’s use geom_smooth() to visualize the same relationship between pct_cancer and pct_asthma from the LA dataframe.
      • Let’s just change the geom to geom_smooth(), which will fit the relationship of our data using a regression, and return one line representing our trend.
LA |>
  ggplot() +
  geom_smooth(aes(x = pct_cancer, y = pct_asthma))

Geometric Objects (adding aesthetics)

  • We can also add aesthetics to this code to delineate geography.
  • Let’s add color and linetype as aesthetics.
LA |>
  ggplot() +
  geom_smooth(aes(x = pct_cancer, y = pct_asthma, color = geography, 
  linetype = geography))

EX_02

  • Using the LA dataframe, can you plot the relationship between the percent of families in a tract living 200% below the federal poverty line (pct_200_fpl), and their transportation burden (transp_burd)?

  • Use a geom_smooth() function

  • Use a color and linewidth aesthetics to visualize the geography variable.

  • Are there big differences between the trend for Louisiana versus Terrebonne Parish? Does it make sense for these variables to have a strong relationship?

Statistical Transformations

  • Allows you calculate new variables to plot without mutating your variable.
  • For geom_bar() function, the default stat is count, meaning that if you supply it with an categoric x column, it will count number of observations in each category.
    • In this case, we can visualize the number of communities n by the number of annual coastal flooding events ann_sf_cat.
LA |> 
  filter(!is.na(ann_cf_cat)) |> 
  ggplot() + 
  geom_bar(aes(x = ann_cf_cat), stat = "count")

Statistical Transformations (.cont)

  • You can also use identity to visualize specific values.
    • In this case, we’ve counted the number of annual coastal flooding events ann_cf_cat using the count() function, and visualized our count n by setting stat to identity.
  • Ultimately, this creates the same graph as the previous one. :-)
LA |>
  filter(!is.na(ann_cf_cat)) |> 
  count(ann_cf_cat) |>
  ggplot() +
  geom_bar(aes(x = ann_cf_cat, y = n), stat = "identity")

Position Adjustments

  • Position adjustments tell data where to go if they might otherwise occupy the same space.
    • In this case, we can create a bar chart visualizing the number of annual coastal flooding events ann_cf_cat, filled with the number of communities in each category of transportation burden transp_burd_cat.
    • The default is stack, but you could also try dodge!
LA |>
    filter(!is.na(ann_cf_cat), !is.na(transp_burd_cat)) |>
  ggplot() +
  geom_bar(aes(x = ann_cf_cat, fill = transp_burd_cat), position = "stack")

Facets

  • Facets are a great way to visualize grouped data that feels cluttered!
  • For example, our very first plot can be made clearer just by adding the facet_wrap() function, and indicating that we want it to create several plots based on geography.
LA |>
ggplot() +
  geom_point(aes(x = pct_cancer, y = pct_asthma, color = geography)) + 
facet_wrap(~geography)

EX 03 + 04

see Posit Assignment!

Logistics

  • Two pieces for Tuesday’s discussion
  • Gwen leading centering activity :-)